//
//  YYERetweetStatusView.swift
//  weibo14
//
//  Created by mac on 16/3/7.
//  Copyright © 2016年 mac. All rights reserved.
//

import UIKit
import SnapKit

class YYERetweetStatusView: UIView {
    
    //拿到约束
    var bottomConstraint: Constraint?
    

    var statusViewModel: YYEStatusViewModel? {
        didSet{
            // 去设置label显示的内容
            // contentLabel.text = statusViewModel?.retweetedText
            contentLabel.attributedText = statusViewModel?.retweetAttributedString

            
            
            // 卸载原来的
            bottomConstraint?.uninstall()
            
            // 重新附的值 找着解决是否隐藏veiw
            if let pics = statusViewModel?.status?.retweeted_status?.pic_urls where pics.count > 0 {

                // 有数据
                pictureView.hidden = false
                // 把你自己需要的数据传过去
                pictureView.picUrls = pics
                
                
                // 更改约束
                self.snp_updateConstraints{ (make) -> Void in
                    bottomConstraint = make.bottom.equalTo(pictureView).offset(YYEStatusCellMargin).constraint
                }
                
            } else {
                // 没有数据
                pictureView.hidden = true
                
                // 更改约束
                self.snp_updateConstraints{ (make) -> Void in
                    bottomConstraint = make.bottom.equalTo(contentLabel).offset(YYEStatusCellMargin).constraint
                }
                
            }
            
            
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // 设置视图
    private func setupUI(){
        backgroundColor = UIColor(white: 239/255, alpha: 1)
        
        // 添加子控件
        addSubview(contentLabel)
        
        addSubview(pictureView)
        // 添加约束
        
        
        
        
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.leading.top.equalTo(self).offset(YYEStatusCellMargin)
            make.trailing.equalTo(self).offset(-YYEStatusCellMargin)
        }
        
        pictureView.snp_makeConstraints { (make) -> Void in
           
            make.leading.equalTo(contentLabel)
            
            make.top.equalTo(contentLabel.snp_bottom).offset(YYEStatusCellMargin)
        }
        
        
   
     
        // 约束转发微博的底部
        self.snp_makeConstraints { (make) -> Void in
           bottomConstraint = make.bottom.equalTo(pictureView).offset(YYEStatusCellMargin).constraint
        }
    
    }
    
    // MARK: - 懒加载控件
    
    // 显示转发微博内容的视图
    private lazy var contentLabel: UILabel = {
        let label = UILabel(textColor: UIColor.darkGrayColor(), fontSzie: 15)
        // 多行
        label.numberOfLines = 0
        label.text = "哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈"
        return label
    }()
    
    // 图片框
    private lazy var pictureView:YYEStatusPictureView = {
        
        let view = YYEStatusPictureView(frame: CGRectZero, collectionViewLayout: UICollectionViewFlowLayout())
        
        return view
        
    }()

    
}



